package com.senditcertified.mail;


import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.Utility.Utility;
import com.activities.UserSecurityActivity;
import com.senditcertified.mail.R;
import com.parser.Error_Report;
import com.parser.Parser_Soap;
import com.soap.call.Http_Soap_Call;

public class MainActivity extends Activity {
	/** Called when the activity is first created. */
	private EditText username,password;
	private Button login;
	ProgressDialog pd;
	ImageView imag;

	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		 requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.login);
		findViewByID();
		login.setOnClickListener(new OnClickListener() {

			
			public void onClick(View v) {
				if (TextUtils.isEmpty(username.getText().toString())) {
					Toast.makeText(MainActivity.this,
							"please enter your email", Toast.LENGTH_SHORT)
							.show();
					return;
							
				}
				else if(!Utility.isEmailValid((username.getText().toString()))){
					Toast.makeText(MainActivity.this,
							"plz enter valid email address", Toast.LENGTH_SHORT)
							.show();
					return;
				}
				new LoginOnlineTask().execute(username.getText().toString());
			}
		});

		//Http_Soap_Call call = Http_Soap_Call.create_connection("", this);
		// parse.GetLoginToken(call.GetLoginToken("zxzx@dd.com", "123456"));
		// parse.GetTime(call.GetTime());
		// call.GetUser("fff", "dfdf");
		// parse.GetUser(call.GetUser("fff", "dfdf"));
		//call.GetUserSecurityImage("dffdfdf");
	}

	private void findViewByID() {
		username = (EditText) findViewById(R.id.username);
		//password= (EditText) findViewById(R.id.password);
		login = (Button) findViewById(R.id.login_btn);
		imag=(ImageView)findViewById(R.id.imag);

	}

	class LoginOnlineTask extends AsyncTask<String, Void, Boolean> {
		boolean result = false;
		//String error_msg = "";
		String Result=null;
		Parser_Soap parse=new Parser_Soap();
		Error_Report repor=new Error_Report();

		
		protected Boolean doInBackground(String... params) {
			Http_Soap_Call call = Http_Soap_Call.create_connection("", MainActivity.this);
			if(call!=null)
			{
				Result= parse.GetUserImage(call.GetUserSecurityImage(params[0],repor),repor);
				if(Result!=null && !Result.equals("")){
					System.out.println("true");
					 result=true;
				}
				else{
					//error_msg=Utility.INVALID_USER;
					System.out.println("false");
					 result=false;
					 if(Result!=null && Result.equals("")){
						 repor.setMessage("Invalid UserEmail");
					 }
					 else{
						 repor.setMessage("Internal Error");
					 }
					 
				}
				
				
			}
			else{
				repor.setMessage(Utility.NETWORK_ISSUE);
				return false;
			}

			return result;
		}

		
		protected void onPreExecute() {
			pd = ProgressDialog.show(MainActivity.this, null,
					getString(R.string.loader));
		}

		protected void onPostExecute(Boolean result) {
			// This method runs in the UI thread
			
			pd.dismiss();
			if(result){
				//Utility.printLog("Login Responce", "Login successfully");
				
				try {
					
					//imag.setImageBitmap(decodedByte);
					Intent mainIntent = new Intent(MainActivity.this,UserSecurityActivity.class);
					mainIntent.putExtra("Username", username.getText().toString());
					mainIntent.putExtra("Image", Result);
					MainActivity.this.startActivity(mainIntent);
					MainActivity.this.finish();
					
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
			else{
				Utility.printLog("Login Responce", "Login Unsuccessfully");
				Toast.makeText(MainActivity.this, repor.getMessage(), Toast.LENGTH_SHORT).show();
			}

		}
	}

}